package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game18;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity18 extends AppCompatActivity  {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private EditText et_game18_carNumber;
    private Button btn_game18_select;
    private  RequestQueue queue;
    private  ProgressDialog show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game18);
         queue = Volley.newRequestQueue(this);

        initView();

        btn_game18_select.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

//                Intent intent = new Intent(GameActivity18.this,Game18DescActivity.class);
//                startActivity(intent);


                show();

                 String val = et_game18_carNumber.getText().toString().trim();

                JSONObject object = new JSONObject();
                try {
                    object.put("carNumber",val);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "carViolation", object.toString(), new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        hide();
                        String val = et_game18_carNumber.getText().toString().trim();
                        JSONObject data = jsonObject.optJSONObject("data");
                        try {
                            String state = data.getString("state");

                            if(state.equals("0")){
                                Toast.makeText(GameActivity18.this, "没有查询到" + val + "车的违章数据！", Toast.LENGTH_SHORT).show();
                                return;
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

//                        使用eventbus 传递数据
                        Game18 json = new Gson().fromJson(jsonObject.toString(), Game18.class);

//                        添加缓存
                        SharedPreferences sharedPreferences = getSharedPreferences("game18", MODE_PRIVATE);
                        sharedPreferences.edit().putString("data",jsonObject +"").commit();


                        //使用粘性事件发送
                        EventBus.getDefault().postSticky(json);
//                      跳转界面
                        Intent intent = new Intent(GameActivity18.this,Game18DescActivity.class);
                        startActivity(intent);


                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                        hide();

                        String val = et_game18_carNumber.getText().toString().trim();
                        Toast.makeText(GameActivity18.this, "没有查询到" + val + "车的违章数据！", Toast.LENGTH_SHORT).show();
                    }
                });

                queue.add(request);


            }
        });


    }

    private void  show(){
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }
    private void hide(){
        show.dismiss();

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        tv_right_text.setVisibility(View.GONE);
        iv_rigth_image.setVisibility(View.GONE);

        tv_content_title.setText("车辆违规");

        tv_left_text.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);

        et_game18_carNumber = (EditText) findViewById(R.id.et_game18_carNumber);
        btn_game18_select = (Button) findViewById(R.id.btn_game18_select);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
